package com.newear.happyDialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.newear.R;
import com.newear.bean.ReportInfo;
import com.newear.util.Validator;

import java.util.ArrayList;

/**
 * Created by happy on 2018/6/1.
 */

public class FaceTougueDialog extends AlertDialog {
    private Context mContext;
    private ImageView closeImageView;
    private ImageView imgFaceTougue;
    private LinearLayout lL_face_tongue_container;
    private ArrayList<String> mTypeList;
    private ArrayList<String> mResultList;
    private String mBtnTitle;
    private Button btn_face_tongue;
    private int type;
    private ReportInfo.FaceResult faceResult;
    private ReportInfo.TongueResult tongueResult;
    private String[] FACE_COLOR = {"脸白", "脸黑", "脸红", "脸黄", "脸青", "正常"};
    private String[] FACE_GLOSS = {"有光泽", "少光泽", "无光泽"};
    private String[] FACE_LIP_COLOR = {"唇淡白", "唇淡红", "唇红", "唇暗红", "唇紫"};

    private String[] TONGUE_NATURE_COLOR = {"舌暗红", "舌淡白", "舌淡红", "舌红", "舌深红（舌紫）"};
    private String[] TONGUE_FAT_THIN = {"正常", "舌胖"};
    private String[] TONGUE_COAT = {"苔黄", "苔白"};
    private String[] TONGUE_COAT_THICKNESS = {"薄苔", "厚苔"};
    private String[] TONGUE_CRACK = {"无裂纹", "有裂纹"};


    public interface Type {
        int FACE = 1;
        int TONGUE = 2;
    }

    public FaceTougueDialog(Context context, int themeResId, String btnTitle, int type) {
        super(context, themeResId);
        mContext = context;
        mBtnTitle = btnTitle;
        this.type = type;
        //点击外面不允许消失
        setCanceledOnTouchOutside(false);
        getWindow().getAttributes().gravity = Gravity.CENTER;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.face_tougue_dialog_layout);
        init();
        findViews();
        setListeners();
        addSheetViews();
    }

    public void setFaceResult(ReportInfo.FaceResult faceResult) {
        this.faceResult = faceResult;
    }

    public void setTongueResult(ReportInfo.TongueResult tongueResult) {
        this.tongueResult = tongueResult;
    }

    private void init(){
        try {
            if (type == Type.FACE) {
                //舌色分析类目
                mTypeList = new ArrayList<String>();
                mTypeList.add("类目");
                mTypeList.add("面色");
                mTypeList.add("光泽");
                mTypeList.add("唇色");
                //结果
                mResultList = new ArrayList<String>();
                mResultList.add("结果");
                if (faceResult != null) {
                    mResultList.add(FACE_COLOR[faceResult.faceColor]);
                    mResultList.add(FACE_GLOSS[faceResult.faceGloss]);
                    mResultList.add(FACE_LIP_COLOR[faceResult.lipColor]);
                }
            } else if (type == Type.TONGUE){
                //舌色分析类目
                mTypeList = new ArrayList<String>();
                mTypeList.add("类目");
                mTypeList.add("舌色");
                mTypeList.add("舌形");
                mTypeList.add("苔色");
                mTypeList.add("苔质");
                mTypeList.add("裂纹");

                //结果
                mResultList = new ArrayList<String>();
                mResultList.add("结果");
                if (tongueResult != null) {
                    mResultList.add(TONGUE_NATURE_COLOR[tongueResult.tongueNatureColor]);
                    mResultList.add(TONGUE_FAT_THIN[tongueResult.tongueFatThin]);
                    mResultList.add(TONGUE_COAT[tongueResult.tongueCoatColor]);
                    mResultList.add(TONGUE_COAT_THICKNESS[tongueResult.tongueCoatThickness]);
                    mResultList.add(TONGUE_CRACK[tongueResult.tongueCrack]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void findViews(){
        closeImageView = (ImageView) findViewById(R.id.img_face_tougue_close);//include_two
        btn_face_tongue = (Button) findViewById(R.id.btn_face_tongue);
        btn_face_tongue.setText(mBtnTitle);
        lL_face_tongue_container = (LinearLayout) findViewById(R.id.lL_face_tongue_container);
        imgFaceTougue = (ImageView) findViewById(R.id.img_face_tougue);
        if (type == Type.FACE) {
            if (Validator.isEffective(faceResult.faceThumbUrl)) {
                Glide.with(mContext).load(faceResult.faceThumbUrl).into(imgFaceTougue);
            }
        } else {
            if (Validator.isEffective(tongueResult.tongueThumbUrl)) {
                Glide.with(mContext).load(tongueResult.tongueThumbUrl).into(imgFaceTougue);
            }
        }
    }

    private void setListeners(){
        closeImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void addSheetViews(){
        if (lL_face_tongue_container != null){
            lL_face_tongue_container.removeAllViews();
        }
        if (mTypeList != null) {
            for (int i = 0; i < mTypeList.size(); i++) {
                View view = View.inflate(mContext, R.layout.report_dialog_face_sheet, null);
                TextView typeTextView = (TextView) view.findViewById(R.id.tv_left_type);
                TextView resultTextView = (TextView) view.findViewById(R.id.tv_right_result);
                view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                lL_face_tongue_container.addView(view);

                typeTextView.setText(mTypeList.get(i));
                resultTextView.setText(mResultList.get(i));
            }
        }
    }



}
